<template>
  <view>
    <view class="container">
      <!-- 表单内容 -->
      <u-form class="form-content">
        <!-- 车牌号码输入 -->
        <u-form-item label="请输入车牌号码" class="form-item" label-width="120">
          <u-input
            v-model="formData.vehicleNum"
            type="text"
            placeholder="请输入车牌号码"
            class="custom-input"
          />
        </u-form-item>

        <!-- 车牌颜色选择 -->
        <u-form-item label="车牌颜色" class="form-item" label-width="120">
          <view class="arrow-icon-groud">
            <u-input
              class="custom-input"
              v-model="formData.plateColorCode"
              placeholder="请选择车牌颜色"
              @click="changeOpenCarColor"
            />
            <!-- 向下/向上箭头图标 -->
            <u-icon
              :name="plateColorPickerVisible ? 'arrow-up' : 'arrow-down'"
              size="20"
              class="arrow-icon"
            />
          </view>

          <area-picker
            ref="carPicker"
            :title="title"
            titleColor="#808080"
            titleFontSize="14px"
            cancelType="icon"
            cancelColor="#999999"
            cancleFontSize="28rpx"
            confirmColor="#2877FF"
            confirmFontSize="14px"
            confirmFontWeight="500"
            itemFontSize="14px"
            @onClose="closeCarPicker"
            @onConfirm="confirmCarPicker"
            :pickerList="list"
          />
        </u-form-item>
      </u-form>

      <!-- 查询按钮 -->
      <view class="btn-container">
        <u-button @click="goToquerycert()">立即查询</u-button>
      </view>
      <view v-if="showgoToquerycert">
        <view class="card" v-for="(item, index) in vehicleList" :key="index">
          <view class="car-info">
            <view class="car-top">
              <view class="car-top-left">
                <up-icon name="photo"></up-icon>
                <text class="car-number">审核年度:{{ item.inspectyear }}</text>
              </view>
            </view>

            <view class="car-bottom" @click="goTopracDetails(item)">
              <text class="transport-code"
                >审核状态：{{ getStateLabel(item.state) }}</text
              >
              <view>
                <up-icon name="arrow-right"></up-icon>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import { changePlatecolor } from "@/common/util_complete.js";
import * as api from "@/common/api/hub_zsyg/inspectionquery/inspectionquery.js";
export default {
  data() {
    return {
      formData: {
        vehicleNum: "", // 车牌号码
        plateColor: "", // 车牌颜色
        plateColorCode: "", // 车牌颜色代码
      },
      title: "车牌颜色选择", // 选择器标题
      plateColorPickerVisible: false, // 选择器可见性
      list: [], // 车牌颜色选择列表
      showgoToquerycert: false, // 是否显示查询结果
      vehicleList: [], // 车辆年审申请列表
    };
  },
  onLoad(options) {
    this.list = changePlatecolor(this.list); // 初始化车牌颜色列表
  },
  methods: {
    getStateLabel(state) {
      switch (state) {
        case 0:
          return "编辑中";
        case 1:
          return "提交审核";
        case 2:
          return "审核通过";
        case 3:
          return "审核不通过";
        case 4:
          return "已归档";
        case 5:
          return "审核中";
        case 9:
          return "作废";
        default:
          return "其他";
      }
    },

    // 设置车牌颜色代码
    setPlateColorCode(code) {
      this.formData.plateColor = code;
    },
    // 打开车牌颜色选择器
    changeOpenCarColor() {
      this.$refs.carPicker._open();
    },
    // 关闭选择器
    closeCarPicker() {
      this.$refs.carPicker._close();
    },
    // 确认选择器选择
    confirmCarPicker(checkedValue) {
      this.formData.plateColorCode = checkedValue.label;
      this.setPlateColorCode(checkedValue.value);
    },

    // 查询车辆年审申请
    async goToquerycert() {
      const params = {
        vehiclenum: "鄂E45286", // 车牌号码
        platecolor: this.formData.plateColor, // 车牌颜色
        page: 1,
        pagesize: 10,
      };

      try {
        uni.showLoading({ title: "加载中..." }); // 显示加载指示器
        const res = await api.getAnnualInspection(params);

        if (res.statusCode == 200) {
          this.vehicleList = res.data.data.list; // 更新车辆年审申请列表
          this.showgoToquerycert = true; // 显示查询结果
        } else {
          uni.showToast({
            title: res.message || "获取车辆列表失败",
            icon: "none",
          });
        }
      } catch (error) {
        uni.showToast({
          title: "获取车辆列表失败",
          icon: "none",
        });
      } finally {
        uni.hideLoading(); // 隐藏加载指示器
      }
    },

    // 跳转到详情页面
    goTopracDetails(item) {
      uni.setStorageSync("vehicleList", this.vehicleList); // 存储车辆列表
      uni.navigateTo({
        url: `/pages/hub_zsyg/inspectionquery/inspectiondetails?applyid=${item.applyid}`,
      });
    },
    // 关闭弹窗
    close() {
      this.show = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  padding: 10px;
  background-color: #f3f6fa;
  height: 100vh;
  box-sizing: border-box;
}

.form-content {
  background-color: #ffffff;
  padding: 0;
  border-radius: 5px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.form-item {
  padding: 0 10px;
  border-bottom: 1px solid #f1f1f1;
}
.custom-input {
  border: none;
  background-color: transparent;
}

.arrow-icon-groud {
  position: relative;
  flex: 1;

  .arrow-icon {
    position: absolute !important;
    right: 20px;
    top: 10px;
  }
}

.btn-container {
  margin-top: 30px;
  text-align: center;
  width: 100%;
  box-sizing: border-box;
  ::v-deep .u-button--info[data-v-461e713c] {
    width: 100%;
    background-color: #2877ff;
    color: #ffffff;
  }
}

.card {
  background-color: #fff;
  border-radius: 10px;
  padding: 15px;
  margin-bottom: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  .car-info {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;

    .car-top {
      display: flex;
      align-items: center;
      justify-content: space-around;
      padding: 10px 0;
      border-bottom: 1px solid #f1f1f1;
      margin-bottom: 10px;
      .car-top-left {
        display: flex;
        align-items: center;
        flex: 1;
        .car-number {
          margin-left: 10px;
          font-size: 16px;
          color: #2877ff;
        }
      }
    }
    .car-bottom {
      flex: 1;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .transport-code {
      font-size: 12px;
      color: #79829c;
    }
    .icon {
      width: 40px;
      height: 40px;
    }
  }
}
</style>
